import Foundation

// 传感器类型
enum SensorType: String, CaseIterable, Identifiable {
    case temperature = "温度"
    case humidity = "湿度"
    case pm25 = "PM2.5"
    case light = "光照"
    case smoke = "烟雾"
    case pressure = "大气压"
    case battery = "电量"
    case motion = "运动"
    
    var id: String { self.rawValue }
    
    var unit: String {
        switch self {
        case .temperature:
            return "°C"
        case .humidity:
            return "%"
        case .pm25:
            return "μg/m³"
        case .light:
            return "lux"
        case .smoke:
            return "ppm"
        case .pressure:
            return "hPa"
        case .battery:
            return "%"
        case .motion:
            return "m/s"
        }
    }
}

// 传感器数据状态
enum SensorStatus {
    case normal
    case warning
    case error
    
    // 默认初始化为normal状态
    init() {
        self = .normal
    }
    
    // 静态方法评估传感器状态
    static func statusFor(type: SensorType, value: Double, appSettings: AppSettings? = nil) -> SensorStatus {
        switch type {
        case .temperature:
            let threshold = appSettings?.temperatureWarningThreshold ?? 28.0
            if value > threshold { return .warning }
            if value > threshold + 7 { return .error }
        case .humidity:
            let threshold = appSettings?.humidityWarningThreshold ?? 80.0
            if value > threshold { return .warning }
            if value > threshold + 10 { return .error }
        case .pm25:
            if value > 35.0 { return .warning }
            if value > 75.0 { return .error }
        case .smoke:
            if value > 20.0 { return .warning }
            if value > 50.0 { return .error }
        default:
            break
        }
        return .normal
    }
}

// 单个传感器数据
struct SensorDataPoint {
    let type: SensorType
    let value: Double
    let timestamp: Date
    let appSettings: AppSettings?
    
    init(type: SensorType, value: Double, timestamp: Date, appSettings: AppSettings? = nil) {
        self.type = type
        self.value = value
        self.timestamp = timestamp
        self.appSettings = appSettings
    }
    
    var status: SensorStatus {
        return SensorStatus.statusFor(type: type, value: value, appSettings: appSettings)
    }
    
    var formattedValue: String {
        switch type {
        case .temperature, .humidity, .battery:
            return String(format: "%.1f", value)
        case .pressure:
            return String(format: "%.0f", value)
        default:
            return String(format: "%.0f", value)
        }
    }
}

// 传感器数据模型
struct SensorDataCollection {
    var temperature: SensorDataPoint
    var humidity: SensorDataPoint
    var pm25: SensorDataPoint
    var light: SensorDataPoint
    var smoke: SensorDataPoint
    var pressure: SensorDataPoint
    var lastUpdated: Date
    
    func getDataFor(type: SensorType) -> SensorDataPoint? {
        switch type {
        case .temperature:
            return temperature
        case .humidity:
            return humidity
        case .pm25:
            return pm25
        case .light:
            return light
        case .smoke:
            return smoke
        case .pressure:
            return pressure
        default:
            return nil
        }
    }
    
    // 获取所有传感器数据点的数组
    var allDataPoints: [SensorDataPoint] {
        return [temperature, humidity, pm25, light, smoke, pressure]
    }
}

// 示例数据
extension SensorDataCollection {
    static var sample: SensorDataCollection {
        return SensorDataCollection(
            temperature: SensorDataPoint(type: .temperature, value: 24.5, timestamp: Date()),
            humidity: SensorDataPoint(type: .humidity, value: 65.0, timestamp: Date()),
            pm25: SensorDataPoint(type: .pm25, value: 28.0, timestamp: Date()),
            light: SensorDataPoint(type: .light, value: 845.0, timestamp: Date()),
            smoke: SensorDataPoint(type: .smoke, value: 12.0, timestamp: Date()),
            pressure: SensorDataPoint(type: .pressure, value: 1013.0, timestamp: Date()),
            lastUpdated: Date()
        )
    }
} 